//
//  CAOrderDetailViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/14.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAOrderDetailViewController.h"
#import "CAOrderDetailHeaderView.h"
#import "CAOrderDetailAddressView.h"
#import "CAOrderDetailShippingView.h"
#import "CAOrderDetailGoodsView.h"
#import "CAOrderDetailGoodsTableViewCell.h"
#import "CAOrderDetailGoodsHeaderView.h"
#import "CAOrderDetailGoodsFooterView.h"
#import "CAReturnGoodsViewController.h"

@interface CAOrderDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) CAOrderDetailHeaderView *headerView;
@property (nonatomic, strong) CAOrderDetailAddressView *addressView;
@property (nonatomic, strong) CAOrderDetailShippingView *shippingView;
@property (nonatomic, strong) CAOrderDetailGoodsView *goodsView;
@property (nonatomic, strong) UITableView *goodsTableView;
@property (nonatomic, strong) CAOrderDetailGoodsHeaderView *goodsHeaderView;
@property (nonatomic, strong) CAOrderDetailGoodsFooterView *footerView;


@end

static NSString *CellIdentify = @"GoodsCell";

@implementation CAOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.containerView];
    [self.containerView addSubview:self.headerView];
    [self.containerView addSubview:self.addressView];
    [self.containerView addSubview:self.shippingView];
    [self.containerView addSubview:self.goodsTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"订单详情";
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.containerView);
        make.height.mas_equalTo(150);
    }];
    
    [_addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.headerView.mas_bottom).offset(10);
        make.height.mas_equalTo(120);
    }];
    
    [_shippingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.addressView.mas_bottom).offset(10);
        make.height.mas_equalTo(150);
    }];
    
    [_goodsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.shippingView.mas_bottom).offset(10);
        make.height.mas_equalTo(_goodsArray.count * 130 + 100);
    }];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.goodsTableView.mas_bottom).offset(50);
    }];
}

#pragma mark get & set
- (UIScrollView *) scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

- (UIView *) containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor clearColor];
    }
    return _containerView;
}

- (CAOrderDetailHeaderView *) headerView {
    if (!_headerView) {
        _headerView = [CAOrderDetailHeaderView new];
        _headerView.orderStatusLabel.text = self.orderDetailDict[@"status_text"];
        _headerView.orderNoLabel.text = [NSString stringWithFormat:@"订单编号：%@",self.orderDetailDict[@"order_sn"]];
        _headerView.orderDateLabel.text = [NSString stringWithFormat:@"下单时间：%@",self.orderDetailDict[@"created_at"]];
    }
    return _headerView;
}

- (CAOrderDetailAddressView *)addressView {
    if (!_addressView) {
        _addressView = [CAOrderDetailAddressView new];
        _addressView.userLabel.text = [NSString stringWithFormat:@"收货人：%@", self.orderDetailDict[@"consignee_name"]];
        _addressView.addressLabel.text = [NSString stringWithFormat:@"收货地址：%@", self.orderDetailDict[@"complete_address"]];
    }
    return _addressView;
}

- (CAOrderDetailShippingView *)shippingView {
    if (!_shippingView) {
        _shippingView = [CAOrderDetailShippingView new];
        if ([self.orderDetailDict[@"express_name"] isEqualToString:@""]) {
            _shippingView.companyLabel.text = @"暂无物流信息";
            _shippingView.companyNoLabel.hidden = YES;
        } else {
            _shippingView.companyLabel.text = [NSString stringWithFormat:@"快递公司：%@", self.orderDetailDict[@"express_name"]];
            _shippingView.companyNoLabel.text = [NSString stringWithFormat:@"快递单号：%@", self.orderDetailDict[@"express_number"]];
        }
    }
    return _shippingView;
}

//- (CAOrderDetailGoodsView *)goodsView {
//    if (!_goodsView) {
//        _goodsView = [CAOrderDetailGoodsView new];
//        _goodsView.goodsArray = self.goodsArray;
//        _goodsView.isUpgradeOrder = [self.orderDetailDict[@"upgrade_order"] integerValue];
//        _goodsView.totalLabel.text = [NSString stringWithFormat:@"商品价格：%@", self.orderDetailDict[@"goods_price"]];
//        _goodsView.freightLabel.text = [NSString stringWithFormat:@"配送费：%@", self.orderDetailDict[@"freight_price"]];
//    }
//    return _goodsView;
//}

- (UITableView *) goodsTableView {
    if (!_goodsTableView) {
        _goodsTableView = [UITableView new];
        _goodsTableView.dataSource = self;
        _goodsTableView.delegate = self;
        [_goodsTableView setBackgroundColor: [UIColor whiteColor]];
        _goodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_goodsTableView registerClass:[CAOrderDetailGoodsTableViewCell class] forCellReuseIdentifier:CellIdentify];
        _goodsTableView.tableHeaderView = self.goodsHeaderView;
        _goodsTableView.tableFooterView = self.footerView;
        _goodsTableView.scrollEnabled = NO;
    }
    return _goodsTableView;
}

- (CAOrderDetailGoodsHeaderView *)goodsHeaderView {
    if (!_goodsHeaderView) {
        _goodsHeaderView = [[CAOrderDetailGoodsHeaderView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, 30}];
        _goodsHeaderView.backgroundColor = [UIColor whiteColor];
    }
    return _goodsHeaderView;
}

- (CAOrderDetailGoodsFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[CAOrderDetailGoodsFooterView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, 90}];
        _footerView.totalLabel.text = [NSString stringWithFormat:@"商品价格：%@", self.orderDetailDict[@"goods_price"]];
        _footerView.freightLabel.text = [NSString stringWithFormat:@"配送费：%@", self.orderDetailDict[@"freight_price"]];
        _footerView.backgroundColor = [UIColor whiteColor];
    }
    return _footerView;
}

#pragma mark UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.goodsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CAOrderDetailGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    NSDictionary *dict = self.goodsArray[indexPath.row];
    
    cell = [[CAOrderDetailGoodsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, dict[@"goods_img"]]]];
    cell.goodsNameLabel.text = dict[@"goods_name"];
    NSArray *attrArray = dict[@"attr"];
    __block NSString *attrStr = @"";
    [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
        attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
    }];
    cell.brandLabel.text = attrStr;
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", dict[@"goods_price"]];
    
    NSInteger status = [self.orderDetailDict[@"status"] integerValue];
    NSInteger isUpgrade = [self.orderDetailDict[@"upgrade_order"] integerValue];
    if (status == 3 && status == 4 && isUpgrade == 0) {
        [[[cell.returnButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSString *orderId = [NSString stringWithFormat:@"%ld", [self.orderDetailDict[@"id"] integerValue]];
            CAReturnGoodsViewController *returnGoodsVC = [CAReturnGoodsViewController new];
            returnGoodsVC.goodsDict = dict;
            returnGoodsVC.orderId = orderId;
            [self.navigationController pushViewController:returnGoodsVC animated:YES];
        }];
    } else {
        cell.returnButton.hidden = YES;
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    float height = self.goodsArray.count * 120 + 150;
//    return height;
    return 130;
}

@end
